//
//  DwtFoodTableViewController.m
//  生活小助手
//
//  Created by dwt on 14-8-9.
//  Copyright (c) 2014年 lxz. All rights reserved.
//

#import "DwtFoodListController.h"
#import "XZBaseTool.h"
#import "DwtFootCookParam.h"
#import "XZFunctionKey.h"
#import "DwtFootCookReturn.h"
#import "DwtFootCookResult.h"
#import "DwtFootCookData.h"
#import "DwtFootCookStep.h"
#import "UIImageView+WebCache.h"
#import "DwtFoodDetailController.h"
#import "MJRefresh.H"
#import "MBProgressHUD+MJ.h"

@interface DwtFoodListController ()
@property (nonatomic,strong)NSMutableArray *footCookDatas;
@property (nonatomic,assign)int pn;
@property (nonatomic,assign)int totalNum;
@end

@implementation DwtFoodListController

-(NSMutableArray *)footCookData{
    if (_footCookDatas==nil) {
        _footCookDatas = [NSMutableArray array];
    }
    return _footCookDatas;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.pn=0;
    DwtFootCookParam *footCategoryParam = [[DwtFootCookParam alloc]init];
    footCategoryParam.key = [XZFunctionKey foodKey];
    footCategoryParam.cid=self.id;

    [XZBaseTool getWithUrl:@"http://apis.juhe.cn/cook/index" parameter:footCategoryParam resultClass:[DwtFootCookReturn class] success:^(DwtFootCookReturn *footCookReturn) {
        
        self.footCookDatas =(NSMutableArray *) footCookReturn.result.data;
        self.totalNum = [footCookReturn.result.totalNum intValue];
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {

        [self.tableView reloadData];
    }];

    
    
    /** 集成刷新控件*/
    [self setupRefresh];
}
#pragma 刷新控件 与 方法---------------------
/**
 *  集成刷新控件
 */
- (void)setupRefresh{
    // 1.添加头部控件的方法
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];

}

-(void)headerRereshing{
    DwtFootCookParam *footCategoryParam = [[DwtFootCookParam alloc]init];
    footCategoryParam.key = [XZFunctionKey foodKey];
    footCategoryParam.cid=self.id;
    footCategoryParam.pn = [NSString stringWithFormat:@"%d",self.pn+=10];
    
    if (self.pn>=self.totalNum) {
        [MBProgressHUD showError:@"没有跟多菜谱了"];
        [self.tableView headerEndRefreshing];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
        });
        return ;
    }
    [XZBaseTool getWithUrl:@"http://apis.juhe.cn/cook/index" parameter:footCategoryParam resultClass:[DwtFootCookReturn class] success:^(DwtFootCookReturn *footCookReturn) {
        
        NSLog(@"success=%@",footCookReturn);
        
        
        // 获得最新的微博frame数组
        NSArray *footCookDatas = footCookReturn.result.data;
        
        // 将新数据插入到旧数据的最前面
        NSRange range = NSMakeRange(0, footCookDatas.count);
        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
        [self.footCookDatas insertObjects:footCookDatas atIndexes:indexSet];
        
         [self.tableView headerEndRefreshing];
        // 重新刷新表格
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
//        _footCookReturn = nil;
         [self.tableView headerEndRefreshing];
        [self.tableView reloadData];
    }];
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.footCookDatas.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DwtFootCookData *footCookData = self.footCookDatas[indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"footCookCell"];
    
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"footCookCell"];;
    }
    
    if (footCookData!=nil) {
        [cell.imageView setImageWithURL:[footCookData.albums lastObject]placeholderImage:[UIImage imageNamed:@"statusdetail_icon_empty_failed"]];
        cell.textLabel.text=footCookData.title;
        cell.detailTextLabel.text=footCookData.imtro;
    }
    

    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    DwtFootCookData *footCookData =  self.footCookDatas[indexPath.row];
    
    DwtFoodDetailController *foodDetailController = [[DwtFoodDetailController alloc]initWithFootCookData:footCookData];
    
    [self.navigationController pushViewController:foodDetailController animated:YES];
}

@end
